#!/bin/bash

echo "Starting setup mongo replica set..."

outip=192.168.0.203

echo "Waiting for master startup.."
until mongo --host ${outip}:27017 --eval 'quit(db.runCommand({ ping: 1 }).ok ? 0 : 2)' &>/dev/null; do
  printf '.'
  sleep 1
done

echo "Waiting for secondary startup.."
until mongo --host ${outip}:27018 --eval 'quit(db.runCommand({ ping: 1 }).ok ? 0 : 2)' &>/dev/null; do
  printf '.'
  sleep 1
done

echo "Waiting for arbiter startup.."
until mongo --host ${outip}:27019 --eval 'quit(db.runCommand({ ping: 1 }).ok ? 0 : 2)' &>/dev/null; do
  printf '.'
  sleep 1
done

echo "Mongo nodes all started.."

echo setup.sh time now: `date +"%T" `
echo "Param values outip=${outip} rs=${RS}  ..."

mongo --host ${outip}:27017 --authenticationDatabase admin -u root -p valuedaas@123 <<EOF
    var cfg = {
        "_id": "${RS}",
        "members": [
            {
                "_id": 0,
                "host": "${outip}:27017",
                "priority" : 999
            },
            {
                "_id": 1,
                "host": "${outip}:27018",
                "priority" : 1
            },
            {
                "_id": 2,
                "host": "${outip}:27019",
                "priority" : 0,
                "arbiterOnly": true
            }
        ]
    };
    rs.initiate(cfg, { force: true });
    rs.reconfig(cfg, { force: true });
EOF

